package com.zy.pro.contact.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.contact.model.Contact;
import com.zy.pro.contact.service.ContactService;
import com.zy.pro.contact.web.form.ContactSearchForm;
import com.zy.utils.UploadHelper;
/**
 * 联系我们管理控制器
 *
 */
@Controller
@RequestMapping("/control/contact/contactController")
public class ContactController extends BaseController {

	private static final Logger logger = Logger.getLogger(ContactController.class);
	
	/**
	 * 联系我们管理服务类
	 */
	@Resource(name = "contactServiceImpl")
	private ContactService contactService;
	

	/**
	 * 系统-显示编辑联系我们管理
	 */
	@RequestMapping("/dispalySysEdit")
	public String dispalySysEdit(Contact contact, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			if(contact != null && contact.getContactId() != null){
				contact = contactService.find(contact.getContactId());
			}else{
				List<Contact> cList = new ArrayList<Contact>();
				cList = contactService.findAll(new ContactSearchForm());
				if(!cList.isEmpty()){
					contact = cList.get(0);
				}
			}
			map.addAttribute("operateStatus", "sysEdit");
			map.addAttribute("contact", contact);
			//TODO 转发页面
			return "/control/system/contact/sysEditContact";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-编辑联系我们管理
	 */
	@RequestMapping("/sysEdit")
	public String sysEdit(Contact contact,String oldContactImage,ModelMap map, HttpServletRequest request) {
		try {
			
			if(contact != null && contact.getContactImage() != null && contact.getContactImage().indexOf("yibu") != -1){
				//将异步上传的Image拷贝到正式路径下面
				String newPath = contact.getContactImage().replaceAll("yibu", "contactImage");
				UploadHelper.copyFile(contact.getContactImage(), newPath, "yibu", request, logger);
				contact.setContactImage(newPath);
				
				//删除旧Image
				//UploadHelper.deletePic(oldContactImage, "contactImage", request, logger);
			}
			
			if(contact != null && contact.getContactId() != null){
				contactService.updateBySelective(contact);
			}else{
				contactService.save(contact);
			}
			return dispalySysEdit(contact, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	
	
	/**
	 * 列出联系我们管理
	 */
	@RequestMapping("/list")
	public String list(ContactSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			
			//创建页面对象
			PageView<Contact> pageView = new PageView<Contact>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(contactService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示添加联系我们管理
	 */
	@RequestMapping("/displayAdd")
	public String displayAdd(Contact contact, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 添加联系我们管理
	 */
	@RequestMapping("/add")
	public String add(Contact contact, ModelMap map) {
		try {
			//TODO 设置contact  eg:主键信息
			
			contactService.save(contact);
			return list(new ContactSearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示编辑联系我们管理
	 */
	@RequestMapping("/displayEdit")
	public String displayEdit(ContactSearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:Contact contact = contactService.find(form.getId());
			//eg:map.addAttribute("entity", contact);
			
			
			//TODO 转发页面
			return "";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 编辑联系我们管理
	 */
	@RequestMapping("/edit")
	public String edit(Contact Contact, ModelMap map) {
		try {
			//TODO:处理联系我们管理
			
			
			contactService.updateBySelective(Contact);
			return list(new ContactSearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	/**
	 * 删除单个联系我们管理
	 */
	@RequestMapping("/deletecontact")
	public String delete(ContactSearchForm form, ModelMap map) {
		try {
			//TODO  eg:contactService.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 批量删除联系我们管理
	 */
	@RequestMapping("/batchDelete")
	public String deletes(ContactSearchForm form, ModelMap map) {
		try {
//			TODO  eg:contactService.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
}
